package model;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Categories {
	private int cid;
	private int uid;
	private String cat_name;
	private int position;
	public Categories(int cid, int uid, String cat_name,
			int position) {
		this.cid = cid;
		this.uid = uid;
		this.cat_name = cat_name;
		this.position = position;
	}
	public Categories() {
	}
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getCat_name() {
		return cat_name;
	}
	public void setCat_name(String cat_name) {
		this.cat_name = cat_name;
	}
	public int getPosition() {
		return position;
	}
	public void setPosition(int position) {
		this.position = position;
	}
	public int getSizeDB(){
		int result = 0;
		PreparedStatement sqlu = null;
		Connection con = new ConnectDB().getConnection();
		try {
			sqlu = con
					.prepareStatement("select COUNT(cid) from categories");
			ResultSet rs = sqlu.executeQuery();
			while (rs.next()) {
				return rs.getInt(1);
			}
			sqlu.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	public String checkName() {
		String name = this.cat_name;
			if(name == null){
				return "Bạn chưa nhập Categories";
			}else{
				name =name.trim();
				if (name.equals("")) {
					return "Bạn chưa nhập Categories";
				}else{
					PreparedStatement sqlp = null;
					Connection connection = new ConnectDB().getConnection();
					try {
						sqlp = connection.prepareStatement("SELECT * FROM categories WHERE cat_name = ?");
						sqlp.setString(1, name);
						ResultSet rs = sqlp.executeQuery();
						while(rs.next() == true){
							return "Categories đã tồn tại.";
						}
					
						connection.close();
					} catch (Exception e) {
						e.printStackTrace();
					}	
					
				}
			}
		return "";
	}
	public String checkNewName() {
		String name = this.cat_name;
			if(name == null){
				return "Bạn chưa nhập Category";
			}else{
				name.trim();
				if (name.equals("")) { return "Bạn chưa nhập Category";}
				else if(this.getCatByName(name).getCid()!=this.getCid()){
					return "Category đã tồn tại. ";
				}
			}
		return "";
	}
	public Categories getCatByName(String catname) {
		catname = (catname == null) ? this.getCat_name() : catname;
		PreparedStatement sqll = null;
		Categories result = null;
		Connection c = new ConnectDB().getConnection();
		try {
			sqll = c.prepareStatement("select * from categories where cat_name = ?");
			sqll.setString(1, catname);
			ResultSet rs = sqll.executeQuery();
			while (rs.next() == true) {
				int cid = rs.getInt(1);
				int uid = rs.getInt(2);
				String n = rs.getString(3);
				int p = rs.getInt(4);
				result = new Categories(cid, uid, n, p);
			}
			sqll.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return result;
	}
	public boolean Create() {
		PreparedStatement sqlr = null;
		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("INSERT INTO categories(uid, cat_name, position) VALUES (? , ?, ?)");
			sqlr.setInt(1, this.uid);
			sqlr.setString(2, this.cat_name);
			sqlr.setInt(3, this.position);
			int number = sqlr.executeUpdate();
			if (number > 0) {
				return true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean upDate() {
		PreparedStatement sqlr = null;
		Connection con = new ConnectDB().getConnection();
		try {
			sqlr = con
					.prepareStatement("UPDATE categories SET  uid = ?,  cat_name = ? , position = ? WHERE cid = ? LIMIT 1");
			sqlr.setInt(1, this.getUid());
			sqlr.setString(2, this.getCat_name());
			sqlr.setInt(3, this.getPosition());
			sqlr.setInt(4, this.getCid());
			int number = sqlr.executeUpdate();
			if (number > 0) {
				return true;
			}
			sqlr.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	public Categories getCatByID(int cid){
		Categories categories = null;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT * FROM categories WHERE cid = ?");
			sqlp.setInt(1, cid);
			ResultSet rs = sqlp.executeQuery();
			while(rs.next() == true){
				int id = rs.getInt(1);
				int uid = rs.getInt(2);
				String cat_name = rs.getString(3);
				int pt = rs.getInt(4);
				categories = new Categories(id, uid, cat_name, pt);
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return categories;
	}
	public int[] getCID(String orby /*n_name */){
		if(orby==null){
			orby="position";
		}else{
			orby=orby.trim();
			if (!((orby.equals("cat_name"))||(orby.equals("uid")) )) {
				orby="position";
			}
		}
		int[] result = new int[this.getSizeDB()];
		int i =0;
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT cid FROM categories ORDER BY "+ orby + " ASC  ");
			ResultSet rs = sqlp.executeQuery();
			while(rs.next() == true){
				result[i] = rs.getInt(1);
				i++;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public String toString (){
		return this.cid +  " " + this.uid + " " +  this.cat_name + " " +  this.position;
	}
	public boolean indexOf(int a[], int b){
		for (int i = 0; i < a.length; i++) {
			if(a[i]==b)return true;
		}
		return false;
	}
	public boolean isCID(String cid) throws IOException{ /*kiem tra tinh hop le cua nid va co nam trong DB hay khong*/
		if(cid==null) return false;
		if(cid.trim().equals(""))return false;
		cid.trim();
		int id = -1;
		try {
			id = Integer.parseInt(cid);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		PreparedStatement sqlp = null;
		Connection connection = new ConnectDB().getConnection();
		try {
			sqlp = connection.prepareStatement("SELECT cid FROM categories where cid= ? ");
			sqlp.setInt(1, id);
			ResultSet rs = sqlp.executeQuery();
			while (rs.next() == true) {
				return true;
			}
			sqlp.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean del(int cid){
		PreparedStatement delpd = null;
		Connection con = new ConnectDB().getConnection();
		try {
			
			delpd = con.prepareStatement("DELETE FROM categories WHERE cid = ?");
			delpd.setInt(1, cid);
			int number = delpd.executeUpdate();
			if (number > 0) {
				return true;
			}
			delpd.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return  false;
	}

	}
